package com.zealcomm.ivcscustomer.app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.Bundle
import androidx.multidex.MultiDex
import com.coder.zzq.smartshow.core.SmartShow
import com.zco.util.ConvertUtil
import com.zco.util.LogUtil
import com.zco.util.ToastUtil
import com.zco.base.ServerCallback
import com.zco.base.annotations.StreamType
import com.zco.base.entity.*
import com.zealcomm.bugly.BuglyUtil
import com.zco.ccs.entity.IDCardAndBankImageBean
import com.zco.ccs.entity.MessageConfirmBean
import com.zco.ivcs.customer.IvcsCustomer
import com.zco.ivcs.customer.IvcsCustomerCallback
import com.zco.ivcs.customer.IvcsCustomerEvent
import com.zco.ivcs.customer.base.entity.CollectedCustomerInfo
import com.zco.ivcs.customer.base.entity.ReplyAiAgentBean
import com.zco.ivcs.customer.base.entity.SubmitIDAndBankInfoResultBean
import com.zealcomm.ivcscustomer.utils.SystemTraceUtil
import com.zealcomm.utils.MapUtil
import org.webrtc.EglBase
import org.webrtc.SurfaceViewRenderer

class IvcsApplication : Application() {
    // bugly 平台的 appId
    private val BUGLY_APP_ID = "4b6e1fae70"

    override fun onCreate() {
        super.onCreate()
        ToastUtil.init(this)
        SmartShow.init(this)
        ConvertUtil.init(this)
        ivcsCustomer = IvcsCustomer(this)
        SystemTraceUtil.init(ivcsCustomer!!)
        val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
        audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
        // 去掉只用扬声器播放
//        audioManager.isSpeakerphoneOn = true
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                LogUtil.i("NFL", activity.javaClass.name + " 启动了")
            }

            override fun onActivityStarted(activity: Activity) {}
            override fun onActivityResumed(activity: Activity) {}
            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {}
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {}
        })
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(base)
        // Thread.currentThread().setUncaughtExceptionHandler { t, e -> LogUtil.i(e.message) }
        BuglyUtil.init(base, BUGLY_APP_ID, true)
        mapUtil = MapUtil(base)
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                LogUtil.i("${activity.javaClass.name} 启动了")
                nowActivity = activity
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityResumed(activity: Activity) {
                nowActivity = activity
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
            }

        })
    }

    companion object {

        private lateinit var mapUtil: MapUtil
        fun startLocation(onRefreshLocationListener: MapUtil.OnRefreshLocationListener) {
            mapUtil.setRefreshLocationListener(onRefreshLocationListener)
            mapUtil.startLocation()
        }


        fun republish() {
            ivcsCustomer!!.republish()
        }

        fun stopLocalStream() {
            ivcsCustomer!!.stopLocalStream()
        }

        @JvmField
        var ivcsCustomer: IvcsCustomer? = null

        @JvmField
        var customerTokenBody:TokenBody? = null


        //          var BASE_URL = "https://ivcs-demo.zealcomm.cn/";
//         var BASE_URL = "https://ivcs-test.zealcomm.cn/";
//        var BASE_URL = "https://ivcs-test-k8s.zealcomm.cn/";
//         var BASE_URL = "https://ivcs-showcase.zealcomm.cn/"
//        var BASE_URL = "https://ivcs-dev.zealcomm.cn/";
        var BASE_URL = "https://ivcs-test-k8s-azure.zealcomm.cn/"

        @JvmField
        var nowActivity: Activity? = null

        @JvmStatic
        fun getUserId(): String? {
            return ivcsCustomer?.userId
        }

        @JvmStatic
        fun registerEvent(ivcsCustomerEvent: IvcsCustomerEvent?) {
            ivcsCustomer!!.registerIvcsEvent(ivcsCustomerEvent)
        }

        @JvmStatic
        fun setBaseUrl(url: String) {
            BASE_URL = url
        }

        @JvmStatic
        val eGLContext: EglBase.Context
            get() = ivcsCustomer!!.eglContext

        @JvmStatic
        fun loginSession(
            name: String?,
            password: String?,
            agency: String?,
            callback: ServerCallback<TokenBody>?
        ) {
            ivcsCustomer!!.init(BASE_URL, name, password, agency, null, callback)
        }

        @JvmStatic
        fun callAgent(
            businessScenarioId: Int,
            formId: String?,
            onlyAudio:Boolean,
            ivcsCustomerCallback: IvcsCustomerCallback?
        ) {
            ivcsCustomer!!.requestSession(
                StreamType.CUSTOMER,
                businessScenarioId.toString(),
                formId,
                onlyAudio,
                ivcsCustomerCallback!!
            )
        }

        @JvmStatic
        fun attachRenderWithLabel(label: String?, renderer: SurfaceViewRenderer?) {
            ivcsCustomer!!.attachRenderWithLabel(label, renderer)
        }

        @JvmStatic
        fun detachRenderWithLabel(label: String?, renderer: SurfaceViewRenderer?) {
            ivcsCustomer!!.detachRenderWithLabel(label, renderer)
        }

        @JvmStatic
        fun sendText(text: String?, ivcsCustomerCallback: IvcsCustomerCallback?) {
            ivcsCustomer!!.sendText(text, null, ivcsCustomerCallback)
        }

        @JvmStatic
        fun sendHandWritingMessage(recipient:String?, stroke:String?) {
            ivcsCustomer?.sendHandWriting(recipient, stroke)
        }

        @JvmStatic
        fun exitSession(ivcsCustomerCallback: IvcsCustomerCallback?) {
            ivcsCustomer!!.exitSession(ivcsCustomerCallback!!)
        }

        @JvmStatic
        fun screenShare(data: Intent, type: String?, ivcsCustomerCallback: IvcsCustomerCallback) {
            ivcsCustomer!!.screenShare(data, type, ivcsCustomerCallback)
        }

        @JvmStatic
        fun sendMessageConfirmation(messageConfirmBean: MessageConfirmBean<*>, toWhom: String?) {
            ivcsCustomer!!.sendMessageConfirmation(messageConfirmBean, toWhom)
        }

        fun replyAiAgent(replayAiAgentBean: ReplyAiAgentBean) {
            ivcsCustomer!!.replyAiAgent(replayAiAgentBean)
        }

        @JvmStatic
        fun mute() {
            ivcsCustomer!!.mute(true, false)
        }

        @JvmStatic
        fun unmute() {
            ivcsCustomer!!.unmute(true, false)
        }

        @JvmStatic
        fun switchCamera() {
            ivcsCustomer!!.switchCamera()
        }

        @JvmStatic
        fun confirmCollect(
            collectedCustomerInfo: CollectedCustomerInfo,
            callback: ServerCallback<SubmitIDAndBankInfoResultBean>
        ) {
            ivcsCustomer!!.confirmCollect(
                collectedCustomerInfo,
                callback
            )
        }

        fun updateIDCardAndBankImage(
            formId: Int,
            idCardAndBankInfo: IDCardAndBankImageBean,
            serverCallback: ServerCallback<Any>
        ) {
            ivcsCustomer!!.updateIDCardAndBankImage(formId, idCardAndBankInfo, serverCallback)
        }

        fun sendCustomMessage(
            customMessage: CustomMessage,
            ivcsCustomerCallback: IvcsCustomerCallback?
        ) {
            ivcsCustomer!!.sendCustomMessage(customMessage, ivcsCustomerCallback)
        }
    }
}